package com.bdqn;

import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import jakarta.annotation.Resource;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import redis.clients.jedis.Jedis;

import java.util.concurrent.TimeUnit;


@SpringBootTest
public class JedisTester {


    private Jedis jedis;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserService userService;

//    @Before
//    public void setup() {
//        jedis = new Jedis("127.0.0.1", 6379);
//        jedis.auth("hjb");
//    }
//
//    @Test
//    public void testString() {
//        jedis.set("name", "ktjiaoyu");
//        System.out.println(jedis.get("name"));
//        jedis.append("name", "czkt");
//        System.out.println(jedis.get("name"));
//        jedis.mset("name", "ktjiaoyu", "age", "17", "add", "cz");
//        jedis.incr("age");
//        System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("add"));
//    }

    @Test
    public void testString() {
        stringRedisTemplate.opsForValue().set("name", "czkt");
        Assert.assertEquals("czkt", stringRedisTemplate.opsForValue().get("name"));
    }

    @Test
    public void testObject() {
        User user = new User("czkt", "123456", 1, null);
        ValueOperations<String, User> operations = redisTemplate.opsForValue();
        operations.set("ktjiaoyu.crm.user.czkt", user);
        User u = operations.get("ktjiaoyu.crm.user.czkt");
        System.out.println("user.usrName" + u.getUsrName());
    }

    @Test
    public void testExpire() throws InstantiationException, InterruptedException {
        User user = new User("cskt", "123456", 1, null);
        ValueOperations<String, User> operations = redisTemplate.opsForValue();
        operations.set("expire", user, 100, TimeUnit.MILLISECONDS);
        Thread.sleep(1000);
        boolean exists = redisTemplate.hasKey("expire");
        if (exists) {
            System.out.println("exists is true");
        } else {
            System.out.println("exists is false");
        }
    }

    @Test
    public void testHast() {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.put("hash", "name", "ktjiaoyu");
        String value = (String) hash.get("hash", "name");
        System.out.println("hash name'value:" + value);
    }

    @Test
    public void testList() {
        ListOperations<String, String> list = redisTemplate.opsForList();
        list.leftPush("list", "accp");
        list.leftPush("list", "bdqn");
        list.leftPush("list", "czkt");
        String value = (String) list.leftPop("list");
        System.out.println("list value:" + value.toString());
    }

//    @Test
//    public void testGetUser(){
//        User user=userService.getUser(2L);
//        System.out.println("user.usrName:"+user.getUsrName());
//    }
}
